package com.taomus.mytools.other.utils;

import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URL;

import org.slf4j.LoggerFactory;
import org.slf4j.helpers.Util;


public final class JarUtils {
	public static JarURLConnection getJarFileInputStream(String jarPath, String filePath) throws IOException {
		StringBuffer path = new StringBuffer("jar:");
		if (jarPath.startsWith("file:")) {
			path.append(jarPath);
		} else {
			path.append("file:");
			if (CommonUtils.isWindows()) {
				path.append("/");
			}
			path.append(jarPath);
			if (filePath.startsWith("/")) {
				path.append("!");
			} else {
				path.append("!/");
			}
		}
		path.append(filePath);
		URL url = new URL(path.toString());
		JarURLConnection jarConnection = (JarURLConnection) url.openConnection();
		return jarConnection;
	}

	public static void main(String[] args) {
		System.out.println(CommonUtils.getClassPath(Util.class));
		System.out.println(CommonUtils.getClassPath(LoggerFactory.class));
		System.out.println(CommonUtils.getClassPath(JarUtils.class));
		System.out.println(CommonUtils.getUserDir());
		System.out.println(CommonUtils.getClassPath());
	}
}
